//
//  ECMineCourseOrderViewController.m
//  Edu_iOS2018
//
//  Created by apple on 2018/1/6.
//  Copyright © 2018年 yanyan. All rights reserved.
//

#import "ECMineCourseOrderViewController.h"
#import "CYUIView_Tabbar.h"
#import "ECUIVC_OrderList.h"

@interface ECMineCourseOrderViewController ()<CYTabbarViewDatasource,UIScrollViewDelegate>
@property(nonatomic,strong)CYUIView_Tabbar *headerView;
@property(nonatomic,strong)UIScrollView *scrollView;// 最底层
@property(nonatomic,strong)ECUIVC_OrderList *uivc_all;
@property(nonatomic,strong)ECUIVC_OrderList *uivc_nopay;
@property(nonatomic,strong)ECUIVC_OrderList *uivc_paid;

@end

@implementation ECMineCourseOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(paySuccess:) name:kNotifactionPaySuccess object:nil];
    self.title = @"课程订单";
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@""style:UIBarButtonItemStylePlain target:self action:nil];

    [self.view addSubview:self.headerView];
    [self.view addSubview:self.scrollView];
    [self addChildViewControllerAtIndex:0];
    [self addChildViewControllerAtIndex:1];

}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear: animated];
    [self.navigationController.navigationBar lt_reset];
}
- (void)paySuccess:(NSNotification *)noti
{
    // 支付完成，跳转到已支付
    DLog(@"支付完成：跳转到：已支付");
    [self setSelectedIndex:2];
    
}
- (void)setSelectedIndex:(NSInteger)selectedIndex
{
    _selectedIndex = selectedIndex;
    [self.headerView setTabbarOffsetX:_selectedIndex];
    self.headerView.defaultSelectedIndex = _selectedIndex;
    [self.headerView reloadData];
    [self.scrollView setContentOffset:CGPointMake(kScreenWidth*_selectedIndex, 0) animated:YES];
}
-(UIScrollView *)scrollView
{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.headerView.frame)+4, kScreenWidth, kScreenHeight - kNavHeight - TABBAR_TITLE_HEIGHT - 4)];
        _scrollView.pagingEnabled = YES;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.delegate = self;
        _scrollView.bounces = NO;
        _scrollView.exclusiveTouch = YES;
        _scrollView.contentSize = CGSizeMake(3 * kScreenWidth, _scrollView.frame.size.height);
    }
    return _scrollView;
}
- (CYUIView_Tabbar *)headerView
{
    WeakSelf(weakSelf);

    if (!_headerView) {
        CGFloat fontSize = 13 * kPercentage;
        _headerView = [[CYUIView_Tabbar alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth , 40)];
        _headerView.backgroundColor = [UIColor clearColor];
        if (self.selectedIndex) {
            _headerView.defaultSelectedIndex = self.selectedIndex;
        }
        _headerView.indicatorAttributes = @{CYTabIndicatorColor:ECTHEMESELECTEDCOLOR,CYTabIndicatorViewHeight:@(3),CYTabBoxBackgroundColor:ECTHEMESELECTEDCOLOR,CYTabIndicatorCornerRadius:@(1.5)};
        _headerView.normalAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize],NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#000000"]};
        _headerView.selectedAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize],NSForegroundColorAttributeName:ECTHEMESELECTEDCOLOR};
        _headerView.selectionType = CYTabbarSelectionIndicator;
        _headerView.needTextGradient = NO;
        _headerView.scrollEnable = NO;
        _headerView.dataSource = self;
        
        _headerView.indicatorEqualTitleWidth = YES;

        UIView *line1View = [[UIView alloc]initWithFrame:CGRectMake((kScreenWidth - 2 )/ 3.0, 10, 1, 20)];
        line1View.backgroundColor = [UIColor colorWithHexString:@"#f5f5f5"];
        [_headerView addSubview:line1View];
        UIView *line2View = [[UIView alloc]initWithFrame:CGRectMake((kScreenWidth - 2) / 3.0 * 2 + 1, CGRectGetMinY(line1View.frame), CGRectGetWidth(line1View.frame), CGRectGetHeight(line1View.frame))];
        line2View.backgroundColor = [UIColor colorWithHexString:@"#f5f5f5"];
        [_headerView addSubview:line2View];
        UIView *bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(_headerView.frame), kScreenWidth, 3)];
        bottomView.backgroundColor = [UIColor colorWithHexString:@"#f5f5f5"];

        [self.view addSubview:bottomView];
    }
    _headerView.block_IndexPathSelected = ^(NSIndexPath * _Nonnull indexPath) {
                [weakSelf.scrollView setContentOffset:CGPointMake(kScreenWidth*indexPath.row, 0) animated:YES];
    };
    
    return _headerView;
}
- (NSArray<NSString *> *)tabbarTitlesForTabbarView:(CYUIView_Tabbar *)tabbarView
{
   
    return @[@"全部",@"待支付",@"已支付"];
}
-(void)addChildViewControllerAtIndex:(NSInteger)index{
    switch (index) {
        case 0:
            {
                if (!_uivc_all) {
                    WeakSelf(weakSelf);
                    _uivc_all = [[ECUIVC_OrderList alloc]init];
                    _uivc_all.view.frame = CGRectMake(0, 0, kScreenWidth, _scrollView.frame.size.height);
                    [_scrollView addSubview:_uivc_all.view];
                    _uivc_all.block_empty = ^{
                      //去选课
                        [weakSelf showVCToChooseCourse];
                    };
                    [self addChildViewController:_uivc_all];
                    
                }
   
            }
            break;
        case 1:
        {
            if (!_uivc_nopay) {
                _uivc_nopay = [[ECUIVC_OrderList alloc]init];
                _uivc_nopay.orderState = @"created";
                _uivc_nopay.view.frame = CGRectMake(kScreenWidth, 0, kScreenWidth, _scrollView.frame.size.height);
                [_scrollView addSubview:_uivc_nopay.view];
                
                [self addChildViewController:_uivc_nopay];
            }
           
        }
            break;
        case 2:
        {
            if (!_uivc_paid) {
                _uivc_paid = [[ECUIVC_OrderList alloc]init];
                _uivc_paid.orderState = @"paid";
                _uivc_paid.view.frame = CGRectMake(kScreenWidth*2, 0, kScreenWidth, _scrollView.frame.size.height);
                [_scrollView addSubview:_uivc_paid.view];
                [self addChildViewController:_uivc_paid];
            }
  
        }
            break;
        default:
            break;
    }
  
}
// 去选课
- (void)showVCToChooseCourse
{
    
    [self.navigationController popViewControllerAnimated:NO];
    UITabBarController *uivc_tab = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    [uivc_tab setSelectedIndex:0];
}
#pragma mark - scrollView delegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat page = (scrollView.contentOffset.x)/self.view.bounds.size.width;
    [self.headerView setTabbarOffsetX:page];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [self scrollViewDidEndScrollingAnimation:scrollView];
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    NSInteger index = scrollView.contentOffset.x/kScreenWidth;
   
    [self addChildViewControllerAtIndex:index];

    ECUIVC_OrderList *page = self.childViewControllers[index];
    if (page.view.superview) {
        return;
    }
    page.view.frame = CGRectMake(kScreenWidth*index, 0, kScreenWidth, kScreenHeight-64-TABBAR_TITLE_HEIGHT);
    [_scrollView addSubview:page.view];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
